package com.gxa.weixing.pc.controller;

import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.ExchangeRecordVO;
import com.gxa.weixing.core.pojo.ForGoodsDetails;
import com.gxa.weixing.core.pojo.GoodsListVO;
import com.gxa.weixing.core.util.ExportUtil;
import com.gxa.weixing.pc.service.ExchangeRecordService;
import com.gxa.weixing.pc.service.ForGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "兑换商品的api")
@RequestMapping("/forGoods")
public class ForGoodsController {

    @Autowired
    private ForGoodsService forGoodsService = null;

    @Autowired
    private ExchangeRecordService exchangeRecordService = null;

    @ApiOperation("获取兑换商品的分页列表（分页查询）")
    @PostMapping("/getForGoodsList")
    public Map<String,Object> getForGoodsList(@RequestBody Map<String,Object> map) throws BusinessException {
        List<Map<String, Object>> list = forGoodsService.getAllDetailsByPage(map);
        Integer count = forGoodsService.getAllDetailsByPageCount(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(list);
        jsonResult.put("count",count);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("添加或者修改兑换商品，添加不需要id，修改需要传递id")
    @PostMapping("/saveForGoods")
    @ApiImplicitParam(name = "details",value = "details",required = true,dataType = "ForGoodsDetails",dataTypeClass = ForGoodsDetails.class,paramType = "body")
    public Map<String,Object> saveForGoods(@RequestBody ForGoodsDetails details) throws BusinessException {
        forGoodsService.saveForGoods(details);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("删除兑换商品，需传递id")
    @PostMapping("/deleteForGoods")
    @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "Long",dataTypeClass = Long.class,paramType = "query")
    public Map<String,Object> deleteForGoods(@RequestParam Long id) throws BusinessException {
        forGoodsService.deleteForGoods(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("分页获取兑换记录")
    @PostMapping("/getByPage")
    @ApiImplicitParam(name = "map",value = "map",required = true,dataType = "Map",dataTypeClass = Map.class,paramType = "body")
    public Map<String,Object> getByPage(@RequestBody Map<String,Object> map) throws BusinessException {
        List<ExchangeRecordVO> byPage = exchangeRecordService.getByPage(map);
        Integer byPageCount = exchangeRecordService.getByPageCount(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.put("count",byPageCount);
        jsonResult.setData(byPage);
        return jsonResult.getDataMap();
    }

    @PostMapping("/export")
    @ApiOperation("商品列表导出接口")
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws BusinessException, IOException {
        // 获取查询到的数据
        List<ExchangeRecordVO> list = exchangeRecordService.getByPage(map);

        // 语雀导出
        ExportUtil.easyExcelExport(list,ExchangeRecordVO.class,"兑换记录","兑换记录sheet1",response);

    }

}
